<?php

    // breadcrumbs
    $this->breadcrumbs=array(
        'Pictures'=>array('index'),
        'Manage',
    );

?>

<div class="page-header">
    <?php echo CHtml::link('<i class="icon-remove icon-white"></i> Delete all unuse pictures', /*$this->createUrl('picture/deleteUnused')*/'#', array(
            'onclick'=>"{deletePictures();}",
            'class'=>'btn btn-primary',
    )) ?>
    <h1>Manage Pictures</h1>
</div>

<?php $this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'picture-grid',
	'dataProvider'=>$model->search(),
	'filter'=>$model,
	'columns'=>array(
		array(
			'name'=>'thumb',
			'type'=>'raw',
			'filter'=>false,
            'sortable'=>false,
            'value'=>'CHtml::image(Yii::app()->baseUrl."/uploads/".$data->cate."/".$data->name.".".$data->ext, $data->name, array(\'width\'=>120))',
		),
		'name',
		'size',
		'ext',
		'cate',
		array(
			'name' => 'status',
			'value' => 'Lookup::item("PictureStatus",$data->status)',
			'filter' => Lookup::items('PictureStatus'),
		),
		array(
			'class'=>'CButtonColumn',
			'template' => '{view}{delete}',
			'deleteButtonImageUrl'=>Yii::app()->baseUrl.'/css/admin/gridview/delete.png',
			'viewButtonImageUrl'=>Yii::app()->baseUrl.'/css/admin/gridview/view.png',
            // Tooltip for view button
            'viewButtonOptions'=>array(
                'rel'=>'tooltip',
                'data-original-title'=>'Details',
                'title'=>'',
            ),
            // Tooltip for delete button
            'deleteButtonOptions'=>array(
                'rel'=>'tooltip',
                'data-original-title'=>'Delete',
                'title'=>'',
            ),
            'buttons'=>array(
                'delete'=>array(
                    'click'=>"function( e ){
                        e.preventDefault();
                        $('#modalUpdate .modal-body').children(':eq(0)').empty(); // Stop auto POST
                        $('#modalUpdate .modal-header h3').html('Delete Confirm');
                        updateModal($(this).attr('href'));
                        $('#modalUpdate').modal('show');
                    }",
                ),
            ),
		),
	),
)); ?>

<!-- Modal for Update -->
<?php $this->beginWidget('zii.widgets.tui.CTuiModal', array(
    'id'=>'modalUpdate',
    'options'=>array(
        'title'=>'Update Picture',
    ),
)); ?>
<div class="modal-body form-wrap"></div>
<?php $this->endWidget(); ?>

<?php
$updateJS = CHtml::ajax(array(
    'url' => "js:url",
    'data' => "js:form.serialize() + action",
    'type' => 'post',
    'dataType' => 'json',
    'success' => "function(data)
    {
        if(data.status == 'failure')
        {
            $('#modalUpdate div.modal-body').html(data.content);
            $('#modalUpdate div.modal-body form input[type=submit]')
                .die() // Stop from re-binding event handlers
                .live('click', function(e){ // Send clicked button value
                e.preventDefault();
                updateModal(false, $(this).attr('name'));
            });
        }
        else
        {
            $('#modalUpdate div.modal-body').html(data.content);
            if(data.status == 'success') // Update all grid views on success
            {
                $('div.grid-view').each(function(){ // Change the selector if you use different class or element
                    $.fn.yiiGridView.update($(this).attr('id'));
                });
            }
            $('#modalUpdate').modal('hide');
            $('#modalUpdate .modal-body').children(':eq(0)').empty();
        }
    }"
)); ?>

<?php
Yii::app()->clientScript->registerScript('updateModal', "function updateModal(url, act)
    {
        var action = '';
        var form = $('#modalUpdate div.modal-body form');
        if(url == false)
        {
            action = '&action=' + act;
            url = form.attr('action');
        }
        {$updateJS}
    }" 
); ?>

<?php
$deleteJS = CHtml::ajax(array(
    'url' => $this->createUrl('picture/deleteUnused'),
    'data' => "js:form.serialize() + action",
    'type' => 'post',
    'dataType' => 'json',
    'success' => "function(data)
    {
        if(data.status == 'failure')
        {
            $('#modalUpdate div.modal-body').html(data.content);
            $('#modalUpdate div.modal-body form input[type=submit]')
                .die() // Stop from re-binding event handlers
                .live('click', function(e){ // Send clicked button value
                e.preventDefault();
                updateModal(false, $(this).attr('name'));
            });
        }
        else
        {
            $('#modalUpdate div.modal-body').html(data.content);
            if(data.status == 'success') // Update all grid views on success
            {
                $('div.grid-view').each(function(){ // Change the selector if you use different class or element
                    $.fn.yiiGridView.update($(this).attr('id'));
                });
            }
            $('#modalUpdate').modal('hide');
            $('#modalUpdate .modal-body').children(':eq(0)').empty();
        }
    }"
)); ?>

<script type="text/javascript">

function deletePictures()
{ 
    <?php echo CHtml::ajax(array(
        'url'=>array('picture/deleteUnused'),
        'data'=> "js:$(this).serialize()",
        'type'=>'post',
        'dataType'=>'json',
        'success'=>"function(data)
        {
            $('#modalUpdate div.modal-body').html(data.content);
            $('div.grid-view').each(function(){
                \$.fn.yiiGridView.update($(this).attr('id')); 
            });
            $('#modalUpdate').modal('show');
            setTimeout(\"$('#modalUpdate').modal('hide') \",2000);
        } ",
    )); ?>
    return false; 
}
</script>